package com.example.myapplication;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

public class Fragment2_MyAdapter extends RecyclerView.Adapter<Fragment2_MyAdapter.ViewHolder> {
    private final List<Contact> mData;

    public Fragment2_MyAdapter(List<Contact> data) {
        this.mData = data;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.fragment_2_item_contact, parent, false); // 使用自定义布局
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Contact contact = mData.get(position);
        holder.textView.setText(contact.getName());
        holder.imageView.setImageResource(contact.getAvatarResId());
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView textView;
        ImageView imageView;

        ViewHolder(View itemView) {
            super(itemView);
            textView = itemView.findViewById(R.id.contact_name);
            imageView = itemView.findViewById(R.id.contact_avatar);
        }
    }
}

/*
package com.example.myapplication;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

public class Fragment2_MyAdapter extends RecyclerView.Adapter<Fragment2_MyAdapter.ViewHolder> {
    private final List<Contact> mData;
    private final Context mContext;

    public Fragment2_MyAdapter(Context context, List<Contact> data) {
        this.mContext = context;
        this.mData = data;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.fragment_2_item_contact, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Contact contact = mData.get(position);
        holder.textView.setText(contact.getName());
        holder.imageView.setImageResource(contact.getAvatarResId());

        // 设置点击事件
        holder.itemView.setOnClickListener(v -> {
            Intent intent = new Intent(mContext, Fragment2_ChatActivity.class);
            intent.putExtra("CONTACT_NAME", contact.getName()); // 传递联系人姓名
            mContext.startActivity(intent);
        });
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView textView;
        ImageView imageView;

        ViewHolder(View itemView) {
            super(itemView);
            textView = itemView.findViewById(R.id.contact_name);
            imageView = itemView.findViewById(R.id.contact_avatar);
        }
    }
}
*/